package com.example.basicquestions.questions.打印文件;
import java.util.*;


/**
 * @author liw
 * @date 2023-03-10
 */
public class Test {

    /**
     * Map<String,List<int[2]>
     *      String: 打印机编号
     *      List: int[2] 数组 0.优先级 1.打印编号
     * @param args
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        sc.nextLine();
        Map<String, List<int[]>> map = new HashMap<>();
        int count = 0;
        for(int i = 0; i < n; i++) {
            String[] line = sc.nextLine().split(" ");
            //向栈中增加
            if(line.length == 3){
                List<int[]> list = new ArrayList<int[]>();
                int[] is = new int[2];
                is[0] = Integer.parseInt(line[2]);
                is[1] = ++count;
                if(map.containsKey(line[1])){
                    list = map.get(line[1]);
                    list.add(is);
                }else{
                    list.add(is);
                }
                map.put(line[1], list);
            //出栈
            }else if(line.length == 2){
                if(!map.containsKey(line[1])){
                    System.out.println("NULL");
                    continue;
                }
                List<int[]> list = map.get(line[1]);
                if(list.size() == 0){
                    System.out.println("NULL");
                    continue;
                }
                //list.sort((o1,o2)->o2[0]-o1[0]);
//                list.sort(new Comparator<int[]>(){
//                    @Override
//                    public int compare(int[] o1, int[] o2) {
//                        return o2[0] - o1[0];
//                    }
//                });
                System.out.println(list.get(0)[1]);
                list.remove(0);
                map.put(line[1],list);

            }

        }
    }
}
